package com.ling.service;

import com.ling.bean.Blog;
import com.ling.bean.BlogExample;
import com.ling.dao.BlogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BlogService {
    private final BlogMapper blogMapper;

    @Autowired
    public BlogService(BlogMapper blogMapper) {
        this.blogMapper = blogMapper;
    }

    //推荐博客
    public List<Blog> recommendBlog(String userId) {
        return blogMapper.getRecommendBlog(userId);
    }

    //添加博客
    public void saveBlog(Blog blog) {
        blogMapper.insertSelective(blog);
    }

    //修改博客
    public void updateBlog(Blog blog) {
        blogMapper.updateByPrimaryKeySelective(blog);
    }

    //删除博客
    public void delBlog(String id) {
        blogMapper.deleteByPrimaryKey(id);
    }

    //查询用户的所有博客,按照添加的时间逆序排列，不包含回收站中的博客,status为1
    public List<Blog> getUserAllBlog(String userId) {
        BlogExample blogExample = new BlogExample();
        //按照置顶逆序，编辑时间逆序
        blogExample.setOrderByClause("t_blog.top DESC, t_blog.editTime DESC");
        //blogstatus=1
        blogExample.createCriteria().andUseridEqualTo(userId).andBlogstatusEqualTo(1);
        return blogMapper.selectByExampleWithBLOBs(blogExample);
    }

    //查询指定博客分类下的所有博客,按照添加的时间逆序排列
    public List<Blog> getUserBlogByType(String blogTypeId) {
        BlogExample blogExample = new BlogExample();
        blogExample.setOrderByClause("t_blog.top DESC, t_blog.editTime DESC");
        blogExample.createCriteria().andBlogtypeidEqualTo(blogTypeId).andBlogstatusNotEqualTo(2);
        return blogMapper.selectByExampleWithBLOBs(blogExample);
    }

    //查询指定id的博客
    public Blog getBlogById(String id) {
        return blogMapper.selectByPrimaryKey(id);
    }

    //查询用户回收站中的博客
    public List<Blog> getUserAllBlogWithTrashcan(String userId) {
        BlogExample blogExample = new BlogExample();
        blogExample.setOrderByClause("t_blog.top DESC, t_blog.editTime DESC");
        blogExample.createCriteria().andUseridEqualTo(userId).andBlogstatusEqualTo(2);
        return blogMapper.selectByExampleWithBLOBs(blogExample);
    }

    //按照关键字搜索指定用户的博客
    public List<Blog> getUserBlogBySearch(String userId, String keyword) {
        keyword="%"+keyword+"%";
        BlogExample blogExample = new BlogExample();
        blogExample.createCriteria().andUseridEqualTo(userId).andBlogtitleLike(keyword);
        return blogMapper.selectByExampleWithBLOBs(blogExample);
    }

    //获取用户最新的5篇文章
    public List<Blog> getUserLatestBlog(String userId) {
        return blogMapper.getUserLatestBlog(userId);
    }

    //获取用户最热门的5篇文章
    public List<Blog> getUserHottestBlog(String userId) {
        return blogMapper.getUserHottestBlog(userId);
    }

    //根据时间获取最新的博客文章，不指定用户
    public List<Blog> getLatestBlogs() {
        BlogExample blogExample = new BlogExample();
        //不指定用户，设置排序字段
        blogExample.createCriteria().andBlogstatusEqualTo(1);
        blogExample.setOrderByClause("editTime DESC");
        return blogMapper.selectByExampleWithBLOBs(blogExample);
    }

    //获取指定领域下的所有文章
    public List<Blog> getAllBlogsByAreaId(String areaId) {
        BlogExample blogExample = new BlogExample();
        blogExample.createCriteria().andAreaidEqualTo(areaId).andBlogstatusEqualTo(1);
        return blogMapper.selectByExampleWithBLOBs(blogExample);
    }
}